/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.manchester.biology.eqhei.implementations;

import edu.manchester.biology.eqhei.interfaces.eQHEI_Evaluation;
import edu.manchester.biology.eqhei.interfaces.eQHEI_Section;
import java.util.ArrayList;


/**
 *
 * @author Michael Kendall
 */
public class eQHEI_EvaluationIMP implements eQHEI_Evaluation{
    private ArrayList<eQHEI_Section> sections = new ArrayList<>();
    private int sectionIndex = -1;
    
    public eQHEI_EvaluationIMP(){
        
    }

    @Override
    public eQHEI_Section next() throws Exception {
        eQHEI_Section rvalue;
        if(sectionIndex >= sections.size()-1){
            sectionIndex = sections.size();
            throw new Exception("Section  index out of bounds on next");
        }
        ++sectionIndex;
        rvalue = sections.get(sectionIndex);
        return rvalue;
    }

    @Override
    public eQHEI_Section previous() throws Exception {
        eQHEI_Section rvalue;
        if(sectionIndex == 0){
            --sectionIndex;
            throw new Exception("Section  index out of bounds on previous");
        }
        --sectionIndex;
        rvalue = sections.get(sectionIndex);
        return rvalue;
    }

    @Override
    public void add(eQHEI_Section attributeSet) {
        sections.add(attributeSet);
    }

    @Override
    public eQHEI_Section get(String name) throws Exception {
        return sections.get(find(name));
    }

    @Override
    public eQHEI_Section get(Integer i) throws Exception {
        return sections.get(i);
    }

    @Override
    public Integer length() {
        return sections.size();
    }

    @Override
    public eQHEI_Section remove(String name) throws Exception {
        return sections.remove(find(name));
    }
    
    private int find(String name) throws Exception{
        eQHEI_Section a[] = new eQHEI_Section[sections.size()];
        sections.toArray(a);
        int i = 0;
        while(i<a.length && !a[i].getName().equalsIgnoreCase(name)){
            ++i;
        }
        if(i<a.length){
            return i;
        }
        else{
            throw new Exception(name + " Not Found");
        }
    }
}
